/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.bean.model.NotifyTemplate;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.NotifyTemplateService;
import com.yami.shop.user.comment.model.UserTagUser;
import com.yami.shop.user.comment.service.UserTagService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;


/**
 *
 *
 * @author lhd
 * @date 2020-07-01 16:13:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/sendTagNotify")
public class PlatformNotifyController {

    private final NotifyTemplateService notifyTemplateService;
    private final UserTagService userTagService;

    /**
     * 分页查询
     * @param page 分页对象
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<NotifyTemplate>> getNotifyTemplatePage(PageParam<NotifyTemplate> page, NotifyTemplate notifyTemplate) {
        IPage<NotifyTemplate> templatePage = notifyTemplateService.pageTagNotify(page);
        return ResponseEntity.ok(templatePage);
    }

    /**
     * 通过id查询
     * @param templateId id
     * @return 单个数据
     */
    @GetMapping("/info/{templateId}" )
    public ResponseEntity<NotifyTemplate> getById(@PathVariable("templateId") Long templateId) {
        NotifyTemplate template = notifyTemplateService.getById(templateId);
        return ResponseEntity.ok(template);
    }

    /**
     * 新增
     * @param notifyTemplate
     * @return 是否新增成功
     */
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:notifyTemplate:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid NotifyTemplate notifyTemplate) {
        if(CollectionUtils.isEmpty(notifyTemplate.getSelTagIds())){
            throw new YamiShopBindException("yami.notify.tag.msg");
        }
        notifyTemplate.setCreateTime(new Date());
        notifyTemplate.setStatus(1);
        notifyTemplate.setSendType(SendType.CUSTOMIZE.getValue());
        notifyTemplate.setTemplateTypes("3");
        notifyTemplateService.saveTagNotify(notifyTemplate);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     * @return 是否修改成功
     */
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:notifyTemplate:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid NotifyTemplate notifyTemplate) {
        return ResponseEntity.ok(notifyTemplateService.updateById(notifyTemplate));
    }

    /**
     * 删除会员的某个标签
     */
    @DeleteMapping("/{templateId}" )
    public ResponseEntity<Boolean> deleteUserTag(@PathVariable Long templateId) {
        return ResponseEntity.ok(notifyTemplateService.removeById(templateId));
    }

}
